$(function(){
    $(".main_image")[0] != undefined ? $(".main_image").banner_round() : '';
    //文章详情右上角菜单
    $('.title .titleToggle').on('click',function(e){
        $('.title-toggle').slideToggle();
        $(document).one("click", function(){
            $(".title-toggle").hide();
        });
        e.stopPropagation();
    });
    //收藏操作
    $(".fct_favorite").on("click",function(){
        function H(){
            return $('.title-toggle').fadeOut();
        }
        if(!_is_login()){H();return;}
        var _self = $(this),_val = 1 - _self.attr("data-val"),_info=$("#art-info");
        if(is_login.uuid == _info.attr("data-uid")){$.mvalidateTip("不能收藏自己的文章哦");H();return;}
        _self.attr("data-val",_val);
        if(_val == 1){
            _self.addClass("col-red");
            $.mvalidateTip("收藏成功");
        }else{
            _self.removeClass("col-red");
            $.mvalidateTip("已取消收藏");
        }
        doRequest("/ajax/cancel_favor_act",{collectId:_info.attr("data-aid"),is_favor:_val},true,function(data){},function(data){$.mvalidateTip(data.errorMsg);});
    });
    //关注操作
    $(".fct_follow").on("click",function(){
        if(!_is_login()){return;}
        var _self = $(this),_val = 1 - _self.attr("data-val"),_info=$("#art-info");
        if(is_login.uuid == _info.attr("data-uid")){$.mvalidateTip("不能关注自己哦");return;}
        _self.attr("data-val",_val);
        if(_val == 1){
            _self.addClass("follow2").find("i").text("已关注");
            $.mvalidateTip("关注成功");
        }else{
            _self.removeClass("follow2").find("i").text("未关注");
            $.mvalidateTip("已取消关注");
        }
        doRequest("/ajax/cancel_fan_act",{author_id:_info.attr("data-uid"),is_fan:_val},true,function(data){},function(data){$.mvalidateTip(data.errorMsg);});
    });
    //点赞操作
    $(".fct_like").on("click",function(){
        if(!_is_login()){return;}
        var _self = $(this),_val = 1 - _self.attr("data-val"),_info=$("#art-info");
        if(_self.attr("data-val") == 1){$.mvalidateTip("已经点赞过该文章了");return;}
        _self.attr("data-val",_val);
        doRequest("/ajax/art_like_num_add",{article_id:_info.attr("data-aid")},true,function(data){
            $.tipsBox({obj:_self,str: "+1",callback: function(){}});
            _self.addClass("icon-praise2");
        },function(data){
            $.mvalidateTip(data.errorMsg);
        });
    });
    //增加阅读量
    $.fn.showRead = function(){
        var _info=$(this),_au_id=_info.attr("data-uid"),_art_id=_info.attr("data-aid");
        if(typeof(is_login.uuid) != "" && is_login.uuid == _au_id){return;}
        doGetRequest("/ajax/art_read_num_add",{article_id:_art_id,uuid:_au_id,stamptime:Math.random()},false,function(data){$(".read_num_show").html(data.readNum);},function(data){});
    }
    $("#art-info")[0] != undefined ? $("#art-info").showRead() : "";
    //打赏列表
    $('.admire a').click(function(){
        $(this).parents('.admire').find('.disnone').stop().toggle();
        $(this).hasClass('a') ? $(this).removeClass('a') : $(this).addClass('a');
        admiretop = $(this).parents('.admire').offset().top;
        $('html,body').stop().animate({scrollTop: admiretop -'50' + 'px' },800);
    });
    //获取到留言区的位置
    $('.bottom .bottom-article-details a.icon-comments').click(function(){
        mtop = $('.sliding-jq').offset().top;
        $('html,body').stop().animate({scrollTop: mtop - '50' + 'px'},800);
    });
    
    /**评论右侧隐藏菜单start*/
    $("body").delegate('.menu-toggle a',"click",function(){
        $(this).parent().fadeOut();
    });
    $('body').delegate(".menuToggle","click",function(e){
        var _tog = $(this).parent().children('.menu-toggle');
        $(".menu-toggle").each(function(index,obj){
            $(this)[0] === _tog[0] ? $(this).stop().toggle(100) : $(this).hide();
        });
        $(document).one("click",function(){
            $(".menu-toggle").hide();
        });
        $(".title-toggle").stop().fadeOut();
        e.stopPropagation();
    });
    $('body').delegate(".menu-toggle","click",function(e){
        e.stopPropagation();
    });
    /**评论右侧隐藏菜单end*/
    //举报相关
    $('body').delegate("._report","click",function(e){
        return $(this).report();
    });
    $("body").delegate("#comment-onload-box li","click",function(){
        return $(this).find("._reply").click();
    });
    $("body").delegate("#comment-onload-box li .name","click",function(e){
        e.stopPropagation();
    });
    $(".comment-content-a").on("click",function(){
        return reply_comment_fn({comment_id:"",commenter_uid:"",commenter_name:""});
    });
    
    //分享窗口
    $.fn.share_art = function(flag){
        var _share_box = $(".share-art-box"),_ainfo = $("#art-info"),
            _content_box = _share_box.find(".share-content-box"),
            _lis = _share_box.find("li"),
            _close = _share_box.find("._close");
        var S = function(){
            _share_box.stop().fadeIn();
            _content_box.animate({'bottom':'0px'},500);
        }
        var F = function(){//移除分享框
            _share_box.stop().fadeOut();
            _content_box.stop().animate({'bottom':'-200px'},500);
        }
        _share_box.one("click",function(){
            return F();//阴影处移除
        });
        _close.on("click",function(){
            return F();
        });
        _content_box.on("click",function(e){
            e.stopPropagation();
        });
        _lis.css("width","50%");
        if(flag){
            _lis.unbind("click").on("click",function(){
                var _li = $(this),_type = _li.attr("data-type");
                var _title = _ainfo.attr("data-title"),_desc = _ainfo.attr("data-desc"),_link = _ainfo.attr("data-link"),_thumb = _ainfo.attr("data-thumb");
                var from = "（分享自@弹溜溜-杭州乌云网络）";
                var z = encodeURIComponent(_title),s = "",u = encodeURIComponent(_link),img = _thumb;
                var r = encodeURIComponent("--弹溜溜，儿时快乐的指尖回忆。在这里发现更多志趣相投的伙伴；参与最前沿的互联网金融体验；探索最酷最好玩的生活..." + (_type == "sina" ? from : "")) ;
                switch(_type){
                    case "sina"://微博
                        e = "http://service.weibo.com/share/share.php?url={0}&title="+ z + s + r +"&pic={1}", n = String.Format(e, u ,img);
                    break;
                    case "qq-space"://qq空间
                        e = "http://sns.qzone.qq.com/cgi-bin/qzshare/cgi_qzshare_onekey?url={0}&title=" + z + "&desc={1}&summary={1}", n = String.Format(e, u, (z + r));
                    break;
                }
                try {
                    window.open(n);
                } catch (u) {
                    window.open(n, t);
                }
                return;
            });
        }
        return S();
    }
    //分享提示
    $.fn.share_msg = function(){
        var _self = $(this);
        _self.on("click",function(){
            return _self.hide();
        });
        return _self.show();
    }
    //分享相关
    $("._share").on("click",function(){
        var brower = brower_info(),_sinfo = $("#art-info");
        var _link = _sinfo.attr("data-link"),_title = _sinfo.attr("data-title"),_desc = _sinfo.attr("data-desc"),_thumb = _sinfo.attr("data-thumb");
        if(brower.is_weixin()){
            return $("#wx-share").share_msg();
        }else if(brower.is_ucBrower() || brower.is_qqBrower()){
            return $(this).share_art(),new native_share("share-box",{url:_link,title:_title,desc:_desc,img:_thumb,img_title:_title,from:"弹溜溜"});
        }else{
            return $(this).share_art(true); 
        }
    });
});



function reply_comment_fn(opt){
    return comment_box_show(opt);
}

function comment_box_show(opt){
    var _content_box = $('.comment-content-box'),
        _bottom_box = $("#bottom-content-box"),
        _text_box = _content_box.find(".public-field-text"),
        _sbtn = _content_box.find("._send"),
        _cbtn = _content_box.find("._cancel"),
        _content = _content_box.find("#comment-content"),
        _ainfo = $("#art-info");
    _sbtn.attr("data-comment-id",opt.comment_id).attr("data-comment-uuid",opt.commenter_uid);
    
    var S = function(){
        if(opt.comment_id != ""){
            _content.val("").attr("placeholder","@"+opt.commenter_name+":");
        }else{
            _content.val("").attr("placeholder","做个有观点的人");
        }
        _content_box.stop().fadeIn(100);
        _text_box.stop().fadeIn(100);
        _content.focus();
        _bottom_box.hide();
    }
    var C = function(){
        _content_box.stop().fadeOut();
        _text_box.stop().fadeOut();
        _content.val("");
        _sbtn.attr("data-comment-id","").attr("data-comment-uuid","");
        return setTimeout(function(){_bottom_box.show();},800);
    }
    _cbtn.on("click",function(){
        return C();
    });
    var sendFlag = true;
    _sbtn.unbind("click").on("click",function(){
        if(!_is_login()){return false;}
        if(!sendFlag){$.mvalidateTip("请不要频繁操作");return;}
        var _self=$(this),_author_id=_ainfo.attr("data-uid"),_article_id=_ainfo.attr("data-aid"),_ctent=_content.val(),
        _novilad=_self.attr("data-disable-val"),_commentid=_self.attr("data-comment-id"),_comment_uid=_self.attr("data-comment-uuid"),_card=0;
        if($.trim(_ctent) == ""){$.mvalidateTip("评论内容不能为空哦！");return;}
        sendFlag = false;_self.addClass("send-load");
        setTimeout(function(){
            doRequest("/ajax/ajax_comment_add",{author_id:_author_id,content:_ctent,article_id:_article_id,comment_id:_commentid,comment_uid:_comment_uid,card:_card},true,function(data){
                sendFlag = true;
                _self.removeClass("send-load");
                $.mvalidateTip("评论成功");
                $("#comment-onload-box").prepend(AnalyticEmotion(data.htmlData));
                $(".icon-comments b").text(data.commentNum);
                return C(),rcomment();
            },function(data){
                sendFlag = true;
                _self.removeClass("send-load");
                $.mvalidateTip(data.errorMsg);
            });
        },1000);
    });
    return S();
}

function rcomment(){
    $li = $("#comment-onload-box li");
    if($li.length > 0){
        $("#noresults").hide();
    }else{
        $("#noresults").show();
        $("#nomoreresults").hide();
    }
}

//个人评论删除操作
function delete_comment_act(opt){
    if(!_is_login()){return;};
    layer.open({
        content: '确认要删除此评论吗？',btn: ['确认', '取消'],yes: function(index){
            doRequest("/ajax/cancel_comment_act",{comment_id:opt.comment_id,stamptime:Math.random()},true,function(resp){
                $(opt.obj).parents("li").remove();$(".icon-comments b").text(resp.commentnum);
                $.mvalidateTip("已经没了");layer.close(index);
                return rcomment();
            },function(data){
                $.mvalidateTip(data.errorMsg);
            });
        }
    });
}
function AnalyticEmotion(s) {
    if(typeof (s) != "undefined") {
        var sArr = s.match(/\[.*?\]/g);
        if(sArr != "" && sArr != null){
            for(var i = 0; i < sArr.length; i++){
                if(uSinaEmotionsHt.containsKey(sArr[i])) {
                    var reStr = "<img src=\"" + uSinaEmotionsHt.get(sArr[i]) + "\" height=\"22\" width=\"22\" />";
                    s = s.replace(sArr[i], reStr);
                }
            }
        }
        
    }
    return s;
}



